// pages/waiter/orderCommit/orderCommit.js
const request = require('@/utils/request.js')
const constants = require('@/utils/khyConstants.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo: {},
    payModeList: getApp().globalData.dictData['payMode'],
    payStateList: getApp().globalData.dictData['payState'],
    imgList:[],// 上传的图片
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPageData()
  },

  getPageData() {
    let orderData = getApp().globalData.pageData[constants.page_orderCommit]
    if (orderData) {
      let obj = {}
      for (const key in orderData) {
        if (orderData.hasOwnProperty(key)) {
          obj['orderInfo.' + key] = orderData[key]
        }
      }
      if (orderData.payMode == '1') {
        obj['payModeName'] = this.data.payModeList[0]['label']
      } else {
        obj['payModeName'] = this.data.payModeList[1]['label']
      }
      if (orderData.payState == '1') {
        obj['payStateName'] = this.data.payStateList[1]['label']
      } else {
        obj['payStateName'] = this.data.payStateList[0]['label']
      }

      if (orderData.goodsPhoto) {
        let goodsPhoto = orderData.goodsPhoto.split(",")
        let imgList=[];
        if(goodsPhoto.length>0){
          goodsPhoto.forEach((item)=>{
              if(item!= ""){
                  imgList.push(getApp().globalData.nginxIp+"/"+item)
              }
          })
          obj['imgList']=imgList
        }
      }

      this.setData(obj)
    }
  },

  maxViewImg(e){
    let imgUrl=e.currentTarget.dataset.img
    wx.previewImage({
        current:imgUrl,
        urls: [imgUrl],
    })
  },

  onManualFinish() {
    let user = getApp().getUserInfo()
    let param = {keyId: this.data.orderInfo.keyId, userNo: user.userNo, userName: user.userName}
    request.get('/UserController/finishOrderById', param).then(res => {
      if (res.data.code == constants.resultSuccessCode) {
        wx.showToast({
          title: '收货成功',
          icon: 'success',
          duration: 2000,
          success: res => {
            setTimeout(function() {
              wx.navigateBack()
            }, 2000)
          }
        })

      } else {
        wx.showToast({
          title: res.data.message || '收货失败',
          icon: 'error',
          duration: 2000
        })
      }
    })
  },

  onScanFinish() {
        let that=this
        wx.scanCode({ //扫描API
            onlyFromCamera: true, //允许使用相册
            needResult: 1,
            scanType: ['barCode','qrCode'], //默认为二维码，barCode条码
            success: function (res) {
              if(res.result == ''){
                wx.showToast({
                    title: '扫描失败',
                    icon: 'error',
                    duration: 2000
                  })
              }else{
                if (that.data.orderInfo.packageNo != res.result) {
                  wx.showToast({
                    title: '扫描单号错误',
                    icon: 'error',
                    duration: 2000
                  })
                } else {
                  let user = getApp().getUserInfo()
                  let data={}
                  data.packageNo=res.result
                  data.userNo=user.userNo
                  data.userName=user.userName
                  request.get('/UserController/finishOrderByPackageNo',data).then(res => {
                    if (res.data.code == constants.resultSuccessCode) {
                        wx.showToast({
                          title: '收货成功',
                          icon: 'success',
                          duration: 2000,
                          success: res => {
                            setTimeout(function() {
                              wx.navigateBack()
                            }, 2000)
                          }
                        })
                
                      } else {
                        wx.showToast({
                          title: res.data.message || '收货失败',
                          icon: 'error',
                          duration: 2000
                        })
                      }
                  })
                }
                
              }
            }
          })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})